package game;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel {
	private boolean seedEnabled = true;
	
	private Action tick = new AbstractAction() {
		@Override
		public void actionPerformed(ActionEvent event) {
			Rules.applyRules(grid);
			repaint();
		}
	};
	
	private Timer timer = new Timer(200, tick);
	
	private Grid grid;
	private int size;

	
	public GamePanel(int size) {
		this.size = size;
		reset();
	}

	public void start() {
		setSeedEnabled(false);
		timer.start();
	}

	public void stop() {
		timer.stop();
	}
	
	private void setSeedEnabled(boolean seedEnabled) {
		this.seedEnabled = seedEnabled;
	}
	
	public void reset() {
		grid = new Grid(size);
		
		setLayout(new GridLayout(size, size));
		
		Cell[][] cells = grid.getCells();
		for (int i = 0; i < cells.length; i++) {
			for (int j = 0; j < cells[0].length; j++) {
				add(cells[i][j]);
			}
		}
		
		setSeedEnabled(true);
	}
}
